//
//  ZJEmptyView.m
//  ZJEmptyViewDemo
//
//  Created by Mr.Zhang on 2019/2/21.
//  Copyright © 2019 Mr.Zhang. All rights reserved.
//

#import "ZJEmptyView.h"

#import <objc/runtime.h>

@interface ZJEmptyView()

@property (nonatomic, strong) UIImageView *bgImageView;

@property (nonatomic, strong) UIButton *clickButton;

@property (nonatomic, assign) EmptyType type;

@end

@implementation ZJEmptyView

-(instancetype)initWithFrame:(CGRect)frame type:(EmptyType)type clickBlock:(void(^)(void))block {
    
    if (self == [super initWithFrame:frame]) {

        self.type = type;
        
        [self initSubViewsClickBlock:block];
    }
    return self;
}

- (void)initSubViewsClickBlock:(void(^)(void))block {
    
    _bgImageView = [[UIImageView alloc]init];
    [self addSubview:_bgImageView];
    
    if (self.type == EmptyType_NoNetWork) {
        _clickButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _clickButton = [[UIButton alloc]init];
        _clickButton.titleLabel.font = [UIFont systemFontOfSize:17];
        [_clickButton setTitle:kLocalizedString(@"重新加载", nil) forState:(UIControlStateNormal)];
        [_clickButton setTitleColor:UIColorFromRGB(0x7889ff) forState:(UIControlStateNormal)];
        _clickButton.layer.cornerRadius = 17.5;
        _clickButton.layer.borderWidth = 0.5;
        _clickButton.layer.borderColor = UIColorFromRGB(0x7889ff).CGColor;
        _clickButton.layer.masksToBounds = YES;
        [_clickButton bk_whenTapped:^{
            if (block != nil) {
                block();
            }
            
        }];
        [self addSubview:_clickButton];
    }
}

-(void)setImageName:(NSString *)imageName {
    
    self.bgImageView.image = [UIImage imageNamed:imageName];
}


-(void)layoutSubviews {
    [super layoutSubviews];
    
    self.bgImageView.frame = CGRectMake(0, 0, self.bgImageView.image.size.width, self.bgImageView.image.size.height);
    self.bgImageView.center = CGPointMake(CGRectGetWidth(self.bounds)/2,  self.type == EmptyType_NoNetWork ? CGRectGetHeight(self.bounds)/2 - 40 : CGRectGetHeight(self.bounds)/2);
    
    if (self.type == EmptyType_NoNetWork) {
        self.clickButton.frame = CGRectMake(58, _bgImageView.bottom + 28, ScreenNarrowSide - 118, 35);
    }
}

@end

